package com.beidouin.GnssMain.models;

/**
 * Created by Liziji on 17/7/12.
 */

import android.text.TextUtils;
import android.util.Log;

import com.beidouin.GnssMain.EventSerialReceive;
import com.beidouin.GnssMain.FileManager;
import com.beidouin.GnssMain.utils.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;


import de.greenrobot.event.EventBus;

import static com.beidouin.GnssMain.utils.Base64utils.getBase64;


/**
 * Created by yuyang on 16/6/22.
 */
public class PilotInfo {

    private static HashMap<String, String> locateTypeMap = new HashMap<String, String>();


    private static PilotInfo sInstance = new PilotInfo();
    private static HashMap<Integer, BeidouSatellite> beidouSatelliteHashMap;
    private String locateType = "未定位";
    private String locateTypeKey;
    private int totalSatelliteNumber;
    private float velocity;
    private float velocityOrientation;
    private String longitude = "--";
    private String latitude = "--";
    private String longitude_FromPC = "--";
    private String latitude_FromPC = "--";
    private String dop = "--";
    private String altitude = "--";
    private String time;
    private String date;

    private String receiveString = " ";
    private String dataString = " ";
    private String handleData = " ";
    int gsaIndex = -1;
    boolean isGPGSA = false;
    private StringBuilder receivedData = new StringBuilder();
    private StringBuilder receivedData_FromPC = new StringBuilder();
    private ArrayList<Byte> BinnaryData = new ArrayList<>();

    public String getDataString() {

        return handleData;
    }

    public String getDate() {
        return date;    //new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    }

    private String latitudeUnit = "";
    private String longitudeUnit = "";
    private String head;
    private String pitch;
    private String roll;
    public String protocol;

    public void setHead(String head) {
        this.head = head;
    }

    public void setPitch(String pitch) {
        this.pitch = pitch;
    }

    public void setRoll(String roll) {
        this.roll = roll;
    }

    public String getHead() {
        return head;
    }

    public String getPitch() {
        return pitch;
    }

    public int getPitchInt() {
        try {
            return Integer.parseInt(pitch) % 180;
        } catch (Exception e) {
            return 0;
        }
    }

    public String getRoll() {
        return roll;
    }

    public void setLatitudeUnit(String latitudeUnit) {
        this.latitudeUnit = latitudeUnit;
    }

    public void setLongitudeUnit(String longitudeUnit) {
        this.longitudeUnit = longitudeUnit;
    }

    public String getLatitudeUnit() {
        return latitudeUnit;
    }

    public String getLongitudeUnit() {
        return longitudeUnit;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTime() {
        return time ;
    }

    public void setAltitude(String altitude) {
        this.altitude = altitude;
    }

    public String getAltitude() {
        if (altitude != null && altitude.length() > 5) return altitude.substring(0, 5) + "m";
        return altitude + "m";
    }

    public void setDop(String dop) {
        this.dop = dop;
    }

    public String getDop() {
        return dop + "";
    }

    public String getLocateType() {
        return locateType;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLongitude_FromPC(String longitude) {
        this.longitude_FromPC = longitude;
    }

    public void setLatitude_FromPC(String latitude) {
        this.latitude_FromPC = latitude;
    }

    public String getLongitude_FromPC() {
        return longitude_FromPC;
    }

    public String getLatitude_FromPC() {
        return latitude_FromPC;
    }

    double x, m, n;
    double Longitude;
    public double getLongitude_double() {
        if (PilotInfo.getInstance().getLongitude() != "--") {
            x = Double.parseDouble(PilotInfo.getInstance().getLongitude());
            m = Math.floor(x / 100);
            n = (double) Math.round((x - m * 100) / 6 * 10 * 1000) / 100000;
            Longitude = m + n;
        }
            return Longitude;
    }
    double Latilude;
    public double getLatitude_double() {
        if (PilotInfo.getInstance().getLongitude() != "--") {
            double x, m, n;

            x = Double.parseDouble(PilotInfo.getInstance().getLatitude());
            m = Math.floor(x / 100);
            n = (double) Math.round((x - m * 100) / 6 * 10 * 1000) / 100000;
            Latilude = m + n;
        }
        return Latilude;
    }

    public double getLongitude_FromPC_double() {
        double x, m, n;
        double Longitude;
        x = Double.parseDouble(PilotInfo.getInstance().getLongitude_FromPC());
        m = Math.floor(x / 100);
        n = (double) Math.round((x - m * 100) / 6 * 10 * 1000) / 100000;
        Longitude = m + n;
        return Longitude;
    }

    public double getLatitude_FromPC_double() {
        double x, m, n;
        double Latilude;
        x = Double.parseDouble(PilotInfo.getInstance().getLatitude_FromPC());
        m = Math.floor(x / 100);
        n = (double) Math.round((x - m * 100) / 6 * 10 * 1000) / 100000;
        Latilude = m + n;
        return Latilude;
    }


    public String getTotalSatelliteNumber() {

        return totalSatelliteNumber + "";
    }

    public String getVelocityStr() {
        return velocity + "";
    }

    public float getVelocity() {
        return velocity;
    }

    public String getVelocityOrientation() {
        return velocityOrientation + "";
    }

    public void setLocateType(String locateType) {

        this.locateType = locateType;
    }

    public void setTotalSatelliteNumber(int totalSatelliteNumber) {
        this.totalSatelliteNumber = totalSatelliteNumber;
    }

    public void setVelocity(float velocity) {
        this.velocity = velocity;
    }

    public void setVelocityOrientation(float velocityOrientation) {
        this.velocityOrientation = velocityOrientation;
    }

    private PilotInfo() {
    }

    public static void init() {
        beidouSatelliteHashMap = new HashMap<Integer, BeidouSatellite>();
        locateTypeMap.put("0", "0 未定位");
        locateTypeMap.put("1", "1 单点定位");
        locateTypeMap.put("2", "2 伪距差分");
        locateTypeMap.put("4", "4 RTK固定解");
        locateTypeMap.put("5", "5 RTK浮点解");
        locateTypeMap.put("6", "6 组合定位");
    }


    public static HashMap<Integer, BeidouSatellite> getBeidouSatelliteHashMap() {
        return beidouSatelliteHashMap;
    }

    public static void initSatelliteHashMap() {
        beidouSatelliteHashMap = new HashMap<Integer, BeidouSatellite>();
    }

    public static void SatelliteMapShiftSecond() {
        int[] ID = new int[100];
        int i = 0;
        for (Map.Entry<Integer, BeidouSatellite> entry : beidouSatelliteHashMap.entrySet()) {
            entry.getValue().shiftSecond();
            if (entry.getValue().getIsReceived() == false) {
                ID[i] = entry.getValue().getMsvId();
                i++;
            }
        }
        for (int j = 0; j < i; j++) {
            beidouSatelliteHashMap.remove(ID[j]);
        }
    }


    public static PilotInfo getInstance() {
        return sInstance;
    }

    public String formatLatLong(String raw) {
        //    if(!raw.contains(".")) return raw + ".";
        //    else return raw.substring(0, raw.indexOf(".")+3);
        //xingsiyu modified
        return raw;
    }

    //xingsiyu modified
    public String transLat(int level, String type) {
        String valid, degree, minute, original, ret = "";
        int iDegree, iMinute;
        double fMinute, fSecond, fValue;
        try {
            if (type.equals("latitude")) {
                original = latitude;
                valid = latitude.substring(0, latitude.indexOf("."));
                degree = latitude.substring(0, valid.length() - 2);   //+"°";
                minute = latitude.substring(latitude.indexOf(".") - 2, latitude.length());
            } else {
                original = longitude;
                valid = longitude.substring(0, longitude.indexOf("."));
                degree = longitude.substring(0, valid.length() - 2); //+"°";
                minute = longitude.substring(longitude.indexOf(".") - 2, longitude.length());
            }
            iDegree = Integer.parseInt(degree);
            fMinute = Double.parseDouble(minute);
            if (level == 0) { //度
                fValue = iDegree + fMinute / 60;
                ret = String.format("%.8f°", fValue);
            } else if (level == 1) { //度分
                ret = String.format("%d°%.6f'", iDegree, fMinute);
            } else { //度分秒
                iMinute = (int) fMinute;
                fSecond = (fMinute - iMinute) * 60;
                ret = String.format("%d°%d'%.4f\"", iDegree, iMinute, fSecond);
            }
            return ret;

        } catch (Exception e) {
            System.out.println("exception=" + latitude + " " + longitude);
            if (type.equals("latitude")) {
                return latitude;
            } else {
                return longitude;
            }
        }

    }

    public String formatTime(String raw) {
        return raw.substring(0, 2) + ":" +
                raw.substring(2, 4) + ":" +
                raw.substring(4, 6);
    }

    //xingsiyu modified
    public String changeTimeToUTC() {
        String strDate = date + " " + time;
        if (strDate == null || strDate.equals("")) return "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("Etc/GMT+0"));//+8
        try {
            Date newdate = formatter.parse(strDate);
            SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
            return formatter2.format(newdate.getTime());
        } catch (ParseException e) {
            System.out.println("Exception :" + e);
            return "";
        }
    }

    public String changeDateToUTC() {
        String strDate = date + " " + time;
        if (strDate == null || strDate.equals("")) return "";
        Date today = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("Etc/GMT+0"));
        try {
            Date newdate = formatter.parse(strDate);
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
            return formatter2.format(newdate.getTime());
        } catch (ParseException e) {
            System.out.println("Exception :" + e);
            return "";
        }
    }


    public void HandleSerialData_Binnary(final byte[] buffer, final int size, int sta) {
        for (int i = 0; i < size; i++) {
            BinnaryData.add(buffer[i]);
        }
        int binnary_size = BinnaryData.size();
        int m = 0, n = 0, p = 0, q = 0;
        int cnt = 0;

        boolean eph_ready = false;
        boolean olink_flag = false;
        for (int i = 0; i < binnary_size; i++) {
            if ((BinnaryData.get(i) == 78) && (BinnaryData.get(i + 1) == 101) && (BinnaryData.get(i + 2) == 119) &&
                    (BinnaryData.get(i + 3) == 115) && (BinnaryData.get(i + 4) == 116) && (BinnaryData.get(i + 5) == 97) && (BinnaryData.get(i + 6) == 114)) {
                if (cnt == 0) {
                    m = i;
                    cnt++;
                } else {
                    n = i;
                    break;
                }
            }
            if ((BinnaryData.get(i) == 0x6f) && (BinnaryData.get(i + 1) == 0x6c) && (BinnaryData.get(i + 2) == 0x69) &&
                    (BinnaryData.get(i + 3) == 0x6e) && (BinnaryData.get(i + 4) == 0x6b)) {
                p = i;
                olink_flag = true;
            }
            if (olink_flag && (BinnaryData.get(i) == 0x24) && (BinnaryData.get(i + 1) == 0x50) && (BinnaryData.get(i + 2) == 0x44) &&
                    (BinnaryData.get(i + 3) == 0x42) && (BinnaryData.get(i + 4) == 0x47) && (BinnaryData.get(i + 5) == 0x4d)) {
                q = i;
                eph_ready = true;
            }

            if (i == (binnary_size - 7)) {
                return;
            }
        }
        byte[] bins = new byte[n - m];
        for (int j = m; j < n; j++) {
            bins[j - m] = BinnaryData.get(j);
        }

        if (eph_ready) {
            byte[] eph = new byte[q - p];
            for (int j = p; j < q; j++) {
                eph[j - p] = BinnaryData.get(j);
            }
            String ss = getBase64(eph);
            OutputEphemeris(ss);
            bins = new byte[p - m];
            for (int j = m; j < p; j++) {
                bins[j - m] = BinnaryData.get(j);
            }
        }
        String s = getBase64(bins);
        OutputBinnary(s);
        ;
        for (int i = m; i < n; i++) {
            BinnaryData.remove(m);
        }
    }


    public void HandleSerialData_FromPC(final byte[] buffer, final int size) {
        receivedData_FromPC.append(new String(buffer, 0, size));
        String s = receivedData_FromPC.toString();
        int n;
        if (s.contains("$PCMDU\n")) {
            n = s.indexOf("$PCMDU\n");
            receivedData_FromPC.delete(n, n + 7);
        }
        int count = Utils.countOccrrence(s, "GPGGA");
        int m;
        if (count <= 1) {
            return;
        }
        s = s.substring(s.indexOf("$GPGGA"), m = s.indexOf("$GPGGA", s.indexOf("$GPGGA") + 6));
        try {
            if (s.indexOf("*") != -1) {
                s = s.substring(0, s.indexOf('*'));
            }
            String[] segmentGngga = s.split(",");
            sInstance.setLatitude_FromPC(formatLatLong(segmentGngga[2]));
            sInstance.setLongitude_FromPC(formatLatLong(segmentGngga[4]));
        } catch (Exception e) {
        }
        receivedData_FromPC.delete(0, m);
        int mask = 0;
        mask |= EventSerialReceive.UPDATE_MAP;
        EventSerialReceive event = new EventSerialReceive(EventSerialReceive.EVENT_UPDATE_ACTIVITY, mask);
        EventBus.getDefault().post(event);
        return;
    }


    public void HandleSerialData(final byte[] buffer, final int size) {

        Boolean[] bChanged = {false, false, false, false, false};    //0 for pilot, 1 for satellite, 2 for ins, 3 for PVT, 4 for NMEA

        receivedData.append(new String(buffer, 0, size));
        String s = receivedData.toString();
        int count = Utils.countOccrrence(s, "([\r\n])");
        if (count <= 1) {
            return;
        }
        //delete half line, xingsiyu modified, not necessary  :)
        s = s.substring(0, s.lastIndexOf("\r\n") + 2);

        String[] mList = s.split("\r\n");

        try {
            for (int i = 0; i < mList.length; i++) {
                String lineInfo = mList[i].trim();
                String oriLine = lineInfo;
                if (lineInfo.indexOf('*') != -1) {
                    lineInfo = lineInfo.substring(0, mList[i].indexOf('*'));

                }
                // GGA协议GPS定位信息状态
                if ((lineInfo.startsWith("$GNGGA") || lineInfo.startsWith("$GPGGA")) && lineInfo.split(",").length > 8) {
                    receiveString = lineInfo;
                    dataString = receiveString + "\r\n";
                    FileManager.getInstance().addRecordTime();
                    OutputLine(oriLine, true, 0);
                    SatelliteMapShiftSecond();
                    String[] segmentGngga = lineInfo.split(",");

                    if (segmentGngga[1] != null && !formatTime(segmentGngga[1]).equals(time)) {
                        bChanged[0] = true;
                        bChanged[1] = true; //maybe too early?  xingsiyu
                        bChanged[3] = true;
                    }
                    if (segmentGngga[1] != null && segmentGngga[1].length() > 6) {
                        sInstance.setTime(formatTime(segmentGngga[1]));
                    }
                    sInstance.setLatitude(formatLatLong(segmentGngga[2]));
                    sInstance.setLongitude(formatLatLong(segmentGngga[4]));
                    // set the E or W to lat and long
                    sInstance.setLatitudeUnit(segmentGngga[3]);
                    sInstance.setLongitudeUnit(segmentGngga[5]);
                    sInstance.setAltitude(segmentGngga[9]);
                    sInstance.setDop(segmentGngga[8]);

                    locateTypeKey = segmentGngga[6];
                    //xingsiyu modified, position flag is 0 & display unpositioned
                    if (locateTypeKey.equals(""))
                        locateTypeKey = "0";
                    String locateType = locateTypeMap.get(locateTypeKey);
                    if (locateType != null) sInstance.setLocateType(locateType);
                    int totalSatellite = Integer.parseInt(segmentGngga[7]);
                    sInstance.setTotalSatelliteNumber(totalSatellite);
                    PointList.getInstance().AddPoint();
                    gsaIndex = 0;
                }
                // RMC协议
                else if ((lineInfo.startsWith("$GPRMC") || lineInfo.startsWith("$GNRMC")) && lineInfo.split(",").length > 10) {
                    receiveString = lineInfo;
                    OutputLine(oriLine, false, 0);
                    String[] segmentGprmc = lineInfo.split(",");
                    dataString += receiveString + "\r\n";

                    if (dataString.startsWith("$GNGGA") || dataString.startsWith("$GPGGA")) {
                        handleData = dataString;
                        dataString = "";
                        bChanged[4] = true;

                    }

                    sInstance.setVelocity(Float.parseFloat(segmentGprmc[7]));
                    if (segmentGprmc[8].equals(""))
                        sInstance.setVelocityOrientation(0);
                    else
                        sInstance.setVelocityOrientation(Float.parseFloat(segmentGprmc[8]));

                    String strDate = segmentGprmc[9];
                    int day, month, year;
                    day = Integer.parseInt(strDate.substring(0, 2));
                    month = Integer.parseInt(strDate.substring(2, 4));
                    year = Integer.parseInt(strDate.substring(4, 6));
                    if (year < 50)
                        year += 2000;
                    else
                        year += 1900;
                    date = String.format("%d-%02d-%02d", year, month, day);

                }
                // GSV协议可见卫星状态
                else if ((lineInfo.startsWith("$BDGSV") ||lineInfo.startsWith("$GBGSV") || lineInfo.startsWith("$GPGSV") || lineInfo.startsWith("$GLGSV") || lineInfo.startsWith("$GAGSV") || lineInfo.startsWith("$GQGSV")) && lineInfo.split(",").length > 6) {
                    receiveString = lineInfo;
                    OutputLine(oriLine, false, 0);
                    String[] segmentGpgsv = lineInfo.split(",");
                    dataString += receiveString + "\r\n";
                    Log.i("dataString", "===============" + dataString);
                    int sateType = -1;

                    if (lineInfo.startsWith("$GBGSV")) {
                        sateType = BeidouSatellite.SATE_BEIDOU;
                    } else if (lineInfo.startsWith("$BDGSV")) {
                        sateType = BeidouSatellite.SATE_BEIDOU;
                    }else if (lineInfo.startsWith("$GPGSV")) {
                        sateType = BeidouSatellite.SATE_GPS;
                    } else if (lineInfo.startsWith("$GLGSV")) {
                        sateType = BeidouSatellite.SATE_GLONASS;
                    } else if (lineInfo.startsWith("$GAGSV")) {
                        sateType = BeidouSatellite.SATE_GALILEO;
                    }else if (lineInfo.startsWith("$GQGSV")){
                        sateType = BeidouSatellite.SATE_QZSS;
                    }

                    if (sateType == -1)
                        continue;

                    // 卫星的详细信息
                    for (int j = 0; j <= 16; j = j + 4) {
                        if (segmentGpgsv.length <= 7 + j) continue;
                        Integer svId = Integer.parseInt(segmentGpgsv[4 + j]);

                        int snr = 0;
                        int elevation = 0;
                        int azimuth = 0;
                        try {
                            snr = (segmentGpgsv[7 + j].equals("")) ? 0 : Integer.parseInt(segmentGpgsv[7 + j]);
                            elevation = (segmentGpgsv[5 + j].equals("")) ? 0 : Integer.parseInt(segmentGpgsv[5 + j]);
                            azimuth = (segmentGpgsv[6 + j].equals("")) ? 0 : Integer.parseInt(segmentGpgsv[6 + j]);
                            if (svId <= 0 || svId > 200 || snr > 60 || elevation < 0
                                    || elevation > 90 || azimuth < 0|| azimuth > 360) {
                                continue;
                            }

                            if (sateType == BeidouSatellite.SATE_GPS && svId >= 193 && svId <= 199) {
                                sateType = BeidouSatellite.SATE_QZSS;
                                svId -= 192;
                            }
                        } catch (Exception e) {
                            continue;
                        }
                        if (beidouSatelliteHashMap.containsKey(GetHashCode(sateType, svId))) {
                            BeidouSatellite currentSatellite = beidouSatelliteHashMap.get(GetHashCode(sateType, svId));
                            currentSatellite.setmSnr(snr);
                            currentSatellite.setmElevation(elevation);
                            currentSatellite.setmAzimuth(azimuth);
                            currentSatellite.setSateType(sateType);
                            currentSatellite.setIsReceived(true);
                        } else {
                            BeidouSatellite beidouSatellite = new BeidouSatellite(svId, snr, elevation, azimuth, sateType);
                            beidouSatelliteHashMap.put(GetHashCode(sateType, svId), beidouSatellite);
                        }
                        if (lineInfo.startsWith("$GPGSV"))
                            sateType = BeidouSatellite.SATE_GPS;

                    }
                }
                // GSA当前卫星信息
                else if (lineInfo.startsWith("$GPGSA") || lineInfo.startsWith("$GNGSA")) {
                    receiveString = lineInfo;
                    OutputLine(oriLine, false, 0);

                    if (isGPGSA && lineInfo.startsWith("$GNGSA") || !isGPGSA && lineInfo.startsWith("$GPGSA")) {
                        isGPGSA = !isGPGSA;
                        getBeidouSatelliteHashMap().clear();
                    }

                    if (lineInfo.indexOf('*') != -1) {
                        lineInfo = lineInfo.substring(0, lineInfo.indexOf('*'));
                    }
                    String[] segmentGngsa = lineInfo.split(",");
                    dataString += receiveString + "\r\n";

                    Log.i("datastring", "========" + oriLine + "==============" + gsaIndex + "==");
                    int sateCnt = 0;
                    int sateType = gsaIndex;
                    if (gsaIndex == 3)
                        sateType = gsaIndex;

                    if (gsaIndex == -1)
                        continue;

                    for (int key = 3; key < 15; key++) {
                        if (segmentGngsa[key].length() == 0)
                            break;
                        Log.i("array", "========" + key + "==============" + segmentGngsa[key] + "======length：" + segmentGngsa.length);
                        int svId = Integer.parseInt(segmentGngsa[key]);
                        //QZSS卫星，网址上兼容老代码
//                        if (sateType == BeidouSatellite.SATE_GPS && svId >= 193 && svId <= 199) {
//                            sateType = BeidouSatellite.SATE_QZSS;
//                            svId -= 192;
//                        } else if (sateType == BeidouSatellite.SATE_GLONASS && svId < 65) {
//                            sateType = BeidouSatellite.SATE_GALILEO;
//                            gsaIndex = BeidouSatellite.SATE_GALILEO;
//                        }

                        if (beidouSatelliteHashMap.containsKey(GetHashCode(sateType, svId))) {
                            BeidouSatellite currentSatellite = beidouSatelliteHashMap.get(GetHashCode(sateType, svId));
                            currentSatellite.isUsed = true;
                            ++sateCnt;
                        }
                        sateType = gsaIndex;
                    }

                    if (lineInfo.startsWith("$GPGSA"))  //马赛克模块
                    {
                        if (gsaIndex != BeidouSatellite.SATE_BEIDOU) {
                            gsaIndex += 1;
                        }
                    } else if (lineInfo.startsWith("$GNGSA"))//中科微模块
                    {
                        if (gsaIndex == BeidouSatellite.SATE_GPS) {
                            gsaIndex = BeidouSatellite.SATE_BEIDOU;
                        } else if (gsaIndex == BeidouSatellite.SATE_BEIDOU)
                            gsaIndex = BeidouSatellite.SATE_GLONASS;
                    }
                    Log.i("glonass", "==========" + oriLine + "==============" + gsaIndex);
                }
            }
        } catch (Exception e) {
            Log.i("error", "==========");
        }
        receivedData.delete(0, s.lastIndexOf("\r\n") + 2);
        int mask = 0;
        if (bChanged[0]) mask |= EventSerialReceive.UPDATE_MAIN;
        if (bChanged[1]) mask |= EventSerialReceive.UPDATE_SATELLITE;
        if (bChanged[3]) mask |= EventSerialReceive.UPDATE_MAP;
        if (bChanged[4]) mask |= EventSerialReceive.UPDATE_NMEA;

        EventSerialReceive event = new EventSerialReceive(EventSerialReceive.EVENT_UPDATE_ACTIVITY, mask);
        EventBus.getDefault().post(event);
        return;
    }

    private void OutputLine(String line, boolean isStart, int type) {
        if (FileManager.getInstance().get_FileStreamSta() == FileManager.ISRECORDING) {
            if (type == 0) {
                if (isStart == true) {
                    FileManager.getInstance().SaveData("[new second]\r\n", true);
                }
                FileManager.getInstance().SaveData(line + "\r\n", false);
            }
        }
    }


    private void OutputBinnary(String line) {
        if (FileManager.getInstance().get_FileStreamSta() == FileManager.ISRECORDING) {
            FileManager.getInstance().SaveData2(line + "\r\n", false);
        }
    }

    private void OutputEphemeris(String line) {
        if (FileManager.getInstance().get_FileStreamSta() == FileManager.ISRECORDING) {
            FileManager.getInstance().SaveData3(line + "\r\n", false);
        }
    }

    private static int GetHashCode(int sateType, int svId) {
        return sateType * 100 + svId;
    }

}